define(function () {
  var sum = function () {
      var $wrap = $('.wrap');
      var $middle = $('#middle');
      var $divs = $('#middle>div');
      var $spans = $('#wrap1>span');
      var $w = $wrap.width();
      $middle.css('width', $w * $divs.length);
      var $index = 0;
      var timer = setInterval($nextFn, 2000);
      function $nextFn() {
          $index++;
          if ($index > $divs.length - 1) {
              $index = 0;
          }
          $middle.css("left", $index * $w * -1);
          $spans.eq($index).css('backgroundColor', '#fff').siblings().css('backgroundColor', 'transparent');
      }

      function $lastFn() {
          $index--;

          if ($index < 0) {
              $index = $divs.length - 1;
          }

          $middle.css("left", $index * $w * -1);
      }

      var startX, endX;
      $wrap[0].addEventListener('touchstart', function (e) {
          var touch = e.targetTouches[0];
          startX = touch.pageX;
      });
      $wrap[0].addEventListener('touchmove', function (e) {
          var touch = e.targetTouches[0];
          endX = touch.pageX;
      })
      $wrap[0].addEventListener('touchend', function (e) {
          if (endX - startX > 100) {
              $lastFn();
              $clearTimer();
          } else if (endX - startX < -100) {
              $nextFn();
              $clearTimer();
          }

      })
      function $clearTimer() {
          clearInterval(timer);
          timer = setInterval($nextFn, 2000);
      }

      $wrap.mouseover(function () {
          clearInterval(timer);
      });
      $wrap.mouseout(function () {
          $clearTimer();
      });
  }
  return {
      sum:sum
  };

})
